<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>{{$router.currentRoute.name}}</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <div style="margin-bottom: 20px;">
      <el-button type="primary" size="medium"
                 @click="$router.push('/sys-admin/serve/show/add-new')">添加车展</el-button>
    </div>

    <el-table :data="tableData" stripe border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
      <el-table-column prop="title" label="标题" width="250" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="description" label="简介" header-align="center" show-overflow-tooltip></el-table-column>

      <el-table-column prop="startTime" label="车展时间(开始)" width="130" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="endTime" label="车展时间(结束)" width="130" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="price" label="车展票价" width="100" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="buyTime" label="购票开始时间" width="130" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="returnTime" label="退票截止时间" width="130" header-align="center" show-overflow-tooltip></el-table-column>

      <el-table-column label="是否启用" width="100" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="toggleEnable(scope.row)"
              v-model="scope.row.enable"
              active-color="#13ce66"
              inactive-color="#ccc"
              :active-value="1"
              :inactive-value="0">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button size="small" icon="el-icon-edit" circle type="primary"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button size="small" icon="el-icon-delete" circle type="danger"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px auto;text-align: right;">
      <el-pagination
          @current-change="changePageNum"
          layout="total, prev, pager, next"
          :total="total"
          :page-size="pageSize"
          :current-page="currentPage">
      </el-pagination>
    </div>

    <el-dialog title="编辑车展" :visible.sync="dialogFormVisible"
               @closed="resetForm('editForm')">
      <el-form :model="editForm" :rules="rules" ref="editForm" label-width="130px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="editForm.title"></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input v-model="editForm.description"></el-input>
        </el-form-item>
        <el-form-item label="车展时间">
          <el-col :span="11">
            <el-date-picker v-model="editForm.startTime" type="datetime" placeholder="开始日期"
                            default-time="00:00:00" value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;">
            </el-date-picker>
          </el-col>
          <el-col style="color: #2c3e50;text-align: center" :span="2">——</el-col>
          <el-col :span="11">
            <el-date-picker v-model="editForm.endTime" type="datetime" placeholder="结束日期"
                            default-time="23:59:59" value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;">
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="车展票价" prop="price">
          <el-input v-model="editForm.price"></el-input>
        </el-form-item>
        <el-form-item label="购票开始时间">
          <el-col :span="11">
            <el-date-picker v-model="editForm.buyTime" type="datetime" placeholder="选择日期"
                            default-time="00:00:00" value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;">
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="退票截止时间">
          <el-col :span="11">
            <el-date-picker v-model="editForm.returnTime" type="datetime" placeholder="选择日期"
                            default-time="23:59:59" value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;">
            </el-date-picker>
          </el-col>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import global from '../../../Global'
let preShowUrl = global.preShows ;

export default {
  data() {
    return {
      tableData: [],
      total: 0,
      pageSize: 0,
      currentPage: 1,
      dialogFormVisible: false,
      editForm: {
      },
      rules: {
        title: [
          { required: true, message: '请输入车展标题', trigger: 'blur' },
          { min: 2, max: 35, message: '长度在 2 到 35 个字符', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '请输入车展开始时间', trigger: 'change' }
        ],
        endTime: [
          { required: true, message: '请输入车展结束时间', trigger: 'change' }
        ],
        price: [
          { required: true, message: '请输入车展票价', trigger: 'blur' }
        ],
        buyTime: [
          { required: true, message: '请输入购票开始时间', trigger: 'change' }
        ],
        returnTime: [
          { required: true, message: '请输入退票截止时间', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 切换启用状态
    toggleEnable(show){
      let now = new Date();
      //车展期间进行禁用，允许禁用但多次提示
      if(show.enable == 0 && new Date(show.buyTime) < now && now < new Date(show.endTime)){
        this.$confirm('【 ' + show.id + '-' + show.title + ' 】车展购票已开始，是否继续禁用?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.submitEnable(show);
        }).catch(() => {
          show.enable = 1;
        });
      }else{
        this.submitEnable(show);
      }
    },
    submitEnable(show){
      let enableText = ['禁用', '启用'];
      let urlMethod = ['disable','enable'];
      let enable = show.enable;
      let url = preShowUrl + "/" + show.id + "/" + urlMethod[enable];

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.$notify.success({title: '成功',
            message: '将【' + show.title + '】的启用状态修改为【' + enableText[enable] + '】成功！'
          });
        }else if(jsonResult.state == 40400){
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadShowList();
            }
          });
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    //车展修改Dialog
    openEditDialog(show){
      let url = preShowUrl + "/" + show.id;

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadShowList();
            }
          });
        }
      });
    },
    //车展修改
    submitEditForm(){
      let url = preShowUrl + "/" + this.editForm.id + "/update";
      let formData = this.qs.stringify(this.editForm);

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.$notify.success({title: '成功',
            message: '编辑车展成功！'
          });

          this.dialogFormVisible = false;
          this.loadShowList();
        }else if(jsonResult.state == 40400){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadShowList();
            }
          });
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    //车展删除确认提示
    openDeleteConfirm(show) {
      this.$confirm('此操作将永久删除【 ' + show.id + '-' + show.title + ' 】, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(show);
      }).catch(() => {
      });
    },
    //车展删除
    handleDelete(show){
      let url = preShowUrl + "/" + show.id + "/delete";

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.$notify.success({title: '成功',
            message: '删除成功'
          });
          this.loadShowList();
        }else if(jsonResult.state == 40400){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadShowList();
            }
          });
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });

    },
    //分页——翻页
    changePageNum(value){
      this.$router.replace("?page="+value);
      this.loadShowList();
    },
    //加载车展列表
    loadShowList(){
      let page = this.$router.currentRoute.query.page;
      if(!page){
        page = 1;
      }
      let url = preShowUrl+"?page="+ page;

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.tableData = jsonResult.data.list;
          this.total = jsonResult.data.total;
          this.pageSize = jsonResult.data.pageSize;
          this.currentPage = jsonResult.data.currentPage;
        }else if(jsonResult.state == 40100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              localStorage.removeItem("localJwt");
              this.$router.push("/login");
            }
          });
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadShowList();
  }
}
</script>
<style>
.el-transfer-panel{
  width:260px !important;
}
</style>